<?php

require_once dirname(__FILE__)."/helper/UserHelper.php";
require_once dirname(__FILE__)."/vo/UserVO.php";
require_once dirname(__FILE__)."/vo/GroupVO.php";
require_once dirname(__FILE__)."/vo/PermissionVO.php";

class UserService
{
	
	private $helper;

	public function beforeFilter($methodName)
	{
		if ($methodName=="login" || $methodName=="logout" ) return true;
		return Authenticate::isAuthenticated();
	}
	
	private function initHelper()
	{
		$this->helper = new UserHelper();
	}

	public function login($login, $pwd)
	{
		//if (Authenticate::isAuthenticated()) return true;
		
		$this->initHelper();

		if($this->helper->checkCredentials($login,$pwd)){
			$isValidUser=true;
		}else{
			$isValidUser=false;
		}

		if ($isValidUser){
			$uid = $this->helper->getIdByLogin($login);
			$roles = implode(",",$this->helper->getRolesById($uid));
			Authenticate::login($uid,$roles);
			return true;
		}
		return false;

	}

	public function logout()
	{
		if (Authenticate::isAuthenticated()) Authenticate::logout();
		return true;
	}
	
	public function getUsersList()
	{
		if(!Authenticate::isUserInRole("read")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		return $this->helper->getAll();
	}
	
	public function getGroupsList()
	{	
		if(!Authenticate::isUserInRole("read")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		return $this->helper->getAllGroups();
	}
	
	public function getPermissionsList()
	{
		if(!Authenticate::isUserInRole("read")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		return $this->helper->getAllPermissions();
	}
	
	public function addNewUser($user)
	{
		if(!(Authenticate::isUserInRole("create") && Authenticate::isUserInRole("update") && Authenticate::isUserInRole("delete") && Authenticate::isUserInRole("read"))){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		$userObj = new UserVO($user);
		$userObj->password = $user["password"];
		
		return $this->helper->addUser($userObj);
	}
	
	public function deleteUserById($userId)
	{
		if(!Authenticate::isUserInRole("delete")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		return $this->helper->deleteUserById($userId);
	}
	
	public function updateUser($user)
	{
		if(!Authenticate::isUserInRole("update")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		$userObj = new UserVO($user);
		$userObj->password = $user["password"];
		
		return $this->helper->updateUser($userObj);
	}
	
	public function addNewGroup($group)
	{
		
		if(!Authenticate::isUserInRole("create")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		$groupObj = new GroupVO($group);
		
		return $this->helper->addGroup($groupObj);
	}
	
	public function deleteGroupById($groupId)
	{
		if(!Authenticate::isUserInRole("delete")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		return $this->helper->deleteGroupById($groupId);
	}
	
	public function updateGroup($group)
	{
		if(!Authenticate::isUserInRole("update")){
			return "Permissions denied!";
		}
		
		$this->initHelper();
		
		$groupObj = new GroupVO($group);
		
		return $this->helper->updateGroup($groupObj);
	}
	

}

?>